<template>
  <div>
    <el-form ref="form" label-width="100px" :model="form" :rules="formRules">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <SxUpload v-model="form.avatar" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { create } from '../../api/employee'
import SxUpload from './SxUpload'
import { checkPhone } from '../../utils/validate'
export default {
  components: {
    SxUpload: SxUpload
  },
  data() {
    return {
      form: {
        name: '',
        password: '',
        avatar: '',
        mobile: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入姓名' },
          { min: 2, max: 5, message: '姓名不合法', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号' },
          { min: 11, max: 11, message: '手机号格式不合法', trigger: 'blur' },
          { validator: checkPhone, message: '手机号格式有误' }
        ],
        password: [
          { required: true, message: '请输入密码' },
          { min: 6, max: 16, message: '密码长度要求在 6 到 16 个字符之间', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请上传头像' }
        ]
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          create(this.form).then(() => {
            console.log('')
            this.$router.push({
              name: 'employee-index'
            })
          })
        } else {
          console.log('fail')
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>
